package com.qlylkj.lbk.wxapi

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.qlylkj.lbk.base.BaseViewModel
import com.qlylkj.lbk.model.api.ResultResponse
import com.qlylkj.lbk.model.repository.WeChatRepository

/**
 *    author : duanguosen
 *    date   : 2019/11/27
 *    desc   :
 */
class WeChatViewModel : BaseViewModel() {

    private val _uiState = MutableLiveData<WeChatUiModel>()
    val uiState: LiveData<WeChatUiModel>
        get() = _uiState
    private val weChatRepository by lazy { WeChatRepository() }

    fun authorizationLogin(authRespCode: String) {
        launch {
            val result = weChatRepository.getAccessToken(authRespCode)
            if (result is ResultResponse.SuccessEntity) {

            } else if (result is ResultResponse.Error) {
                emitUiState(showError = result.exception.message)
            }
        }
    }


    private fun emitUiState(
        showError: String? = null,
        isFinish: Boolean = false
    ) {
        val uiModel = WeChatUiModel(showError, isFinish)
        _uiState.value = uiModel
    }

    data class WeChatUiModel(
        val showError: String?,
        val isFinish: Boolean
    )

}